package com.lancoo.answer.ui.ques.quesBody;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.lancoo.answer.R;
import com.lancoo.answer.model.AnswerConstant;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.bean.Ques;
import com.lancoo.answer.model.bean.Type;
import com.lancoo.answer.util.DpUtils;
import com.lancoo.answer.util.ExamFontSizeAdjustUtils;

import java.util.List;

/**
 * Created by pengmi on 2021/12/16 9:30
 * Description:大题题干内容为空的fragment
 */
public class EmptyQuseBodyFragment extends Fragment {

    private static final String TAG = "EmptyQuseBodyFragment";
    private Ques ques;
    private TextView textView;


    public static Fragment loadQueseFragment(int kindIndex, int queseIndex){
        EmptyQuseBodyFragment queseFragment=new EmptyQuseBodyFragment();
        Bundle mBundle=new Bundle();
        mBundle.putInt(AnswerConstant.KEY_TYPEINDEX,kindIndex);
        mBundle.putInt(AnswerConstant.KEY_QUESINDEX,queseIndex);
        queseFragment.setArguments(mBundle);
        return queseFragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.ev_fragment_ques_body,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (getArguments() == null || getContext() == null || getActivity() == null) {
            return;
        }
        if(ConstantBean.Companion.getConstantExamBean()==null){
            return;
        }
        List<Type> kindBeanListk = ConstantBean.Companion.getConstantExamBean().getTypeList();
        if (kindBeanListk == null) {
            return;
        }
        int typeIndex = getArguments().getInt(AnswerConstant.KEY_TYPEINDEX);
        int quesIndex = getArguments().getInt(AnswerConstant.KEY_QUESINDEX);
        int childIndex = getArguments().getInt(AnswerConstant.KEY_CHILDINDEX);
        if (kindBeanListk.get(typeIndex) == null ||
                kindBeanListk.get(typeIndex).getQuesList() == null) {
            return;
        }
        if(kindBeanListk.get(typeIndex).getQuesList()==null){
            return;
        }
        List<Ques> quesList = kindBeanListk.get(typeIndex).getQuesList();
        ques = quesList.get(quesIndex);
        if (!TextUtils.isEmpty(ques.getAudioSubLeader()))
        addListenTextView(view.findViewById(R.id.framelayout));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    private void addListenTextView(FrameLayout frameLayout){
        if(getContext()==null){
            return;
        }
        frameLayout.removeAllViews();
        textView = new TextView(getContext());
        textView.setText(Html.fromHtml(ques.getAudioSubLeader()));
        Log.e("eeee","addListenTextView:"+ques.getAudioSubLeader());
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
        adjustFontSize();
        int dp12=DpUtils.dip2px(getContext(),12);
        int dp20=DpUtils.dip2px(getContext(),16);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//        params.leftMargin = DpUtils.dip2px(getContext(),12);
        textView.setLayoutParams(params);
        textView.setTextColor(Color.parseColor("#333333"));
        textView.setPadding(dp12,dp20,dp12,dp12);
        textView.setVisibility(View.VISIBLE);
        textView.setLineSpacing(5f,1f);
        frameLayout.addView(textView);
    }

    public void adjustFontSize(){
        if(textView!=null){
            ExamFontSizeAdjustUtils.adjustTextViewFontSize(textView,14);
        }
    }
}
